﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.ServiceModel;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using DevExpress.XtraEditors;
using FileManager_Client.BusinessLogic.NotifyMessageService;
using FileManager_Client.Utility;
using WHC.WareHouseMis.UI;
using Aptech.UI;


namespace FileManager_Client.PresentationLayer
{
    public partial class MainForm : Form, INotifyMessage_ServiceCallback
    {
        private Login _loginWinform;
        private ServerFiles fwq;
        public MainForm(Login login)
        {
            InitializeComponent();
            //双工通讯
            var instance = new InstanceContext(this);
            var service = new NotifyMessage_ServiceClient(instance);
            CacheGlobal.Add(GlobalString.System_Config_String.Wcf_NotifyServiceCacheKey, service);
            var result =
                CacheGlobal.GetCache(GlobalString.System_Config_String.UserSession) as
                BusinessLogic.FileManagerUserService.UserLoginResult;
            var subScriberArgs = new UserLoginResult
            {
                CellPhonek__BackingField = result.CellPhonek__BackingField,
                CompanyIdk__BackingField = result.CompanyIdk__BackingField,
                DiskLargek__BackingField = result.DiskLargek__BackingField,
                Dutyk__BackingField = result.Dutyk__BackingField,
                Emailk__BackingField = result.Emailk__BackingField,
                ExtensionData = result.ExtensionData,
                LoginIdk__BackingField = result.LoginIdk__BackingField,
                OrderNumberk__BackingField = result.OrderNumberk__BackingField,
                PeopleNamek__BackingField = result.PeopleNamek__BackingField,
                Sexk__BackingField = result.Sexk__BackingField,
                Sidk__BackingField = result.Sidk__BackingField,
                UserIdk__BackingField = result.UserIdk__BackingField
            };
             service.Subscriber(subScriberArgs);
            _loginWinform = login;
            skinEngine1.SkinFile = GlobalString.System_Config_String.SkinFileString;
            ShowInTaskbar = false;
            WindowState = FormWindowState.Minimized;
          // tr = new Timer();
          //  tr.Tick += tr_Tick;
       //     tr.Interval = 5000;
     //       tr.Enabled = true;
            StartPosition = FormStartPosition.CenterScreen;
            fwq = new ServerFiles();
            fwq.Show();
        }

        void tr_Tick(object sender, EventArgs e)
        {
            var n = new NotifyWindow { Title = "新消息", WaitTime1 = 5000, ActualHeight1 = 186, ActualWidth1 = 280, Text = _message };
            _message = string.Empty;
            n.Notify();
            tr.Enabled = false;
        }
        private void sideBar1_ItemClick(SbItemEventArgs e)
        {
            var fwq = e.Item.Tag as ServerFiles;
            if (fwq != null) fwq.Show();
        }


        private void 服务器文件ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            var fwq = new ServerFiles();
            fwq.Show();
        }

        private Timer tr = null;

        private void 更换用户ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
            fwq.Hide();
            _loginWinform = new Login();
            _loginWinform.Show();
        }

        private void 退出系统ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var service = CacheGlobal.GetCache(GlobalString.System_Config_String.Wcf_NotifyServiceCacheKey) as NotifyMessage_ServiceClient;
            if(null != service)
            {
                var result =
    CacheGlobal.GetCache(GlobalString.System_Config_String.UserSession) as
    BusinessLogic.FileManagerUserService.UserLoginResult;
                var subScriberArgs = new UserLoginResult
                {
                    CellPhonek__BackingField = result.CellPhonek__BackingField,
                    CompanyIdk__BackingField = result.CompanyIdk__BackingField,
                    DiskLargek__BackingField = result.DiskLargek__BackingField,
                    Dutyk__BackingField = result.Dutyk__BackingField,
                    Emailk__BackingField = result.Emailk__BackingField,
                    ExtensionData = result.ExtensionData,
                    LoginIdk__BackingField = result.LoginIdk__BackingField,
                    OrderNumberk__BackingField = result.OrderNumberk__BackingField,
                    PeopleNamek__BackingField = result.PeopleNamek__BackingField,
                    Sexk__BackingField = result.Sexk__BackingField,
                    Sidk__BackingField = result.Sidk__BackingField,
                    UserIdk__BackingField = result.UserIdk__BackingField
                };
                service.UnSubscriber(subScriberArgs);
            }

            Environment.Exit(0);
        }

        private void 服务器资源ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            fwq.Show();
        }

        private void MyNotifyIcon_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left) return;
            var p1 = MousePosition;
            contextMenuStrip1.Show(p1);
        }

        private void 文件柜设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var cf = new SysConfig();
            cf.Show();
        }

        private string _message; 
        public void NotifyFileMessage(string message)
        {
            //_message = message;
            //    tr = new Timer();
            // tr.Tick += tr_Tick;
            //    tr.Interval = 5000;
            //       tr.Enabled = true;
                   var n = new NotifyWindow
                               {
                                   Title = "新消息",
                                   WaitTime1 = 5000,
                                   ActualHeight1 = 186,
                                   ActualWidth1 = 280,
                                   Text = message
                               };
                   n.Notify();
        }

        private NotifyMessageObject _notifyMessageObject;
        public void NotifyFileMessageOpenFolder(NotifyMessageObject notifyMessageObject)
        {
            var notifyMessage = string.Format(GlobalString.System_Config_String.NotifyMessageOpenFolderStr,
                                              notifyMessageObject.UserNamek__BackingField,
                                              notifyMessageObject.FileNamek__BackingField);
            var n = new NotifyWindow
                        {
                            Title = "通知!",
                            WaitTime1 = 5000,
                            ActualHeight1 = 186,
                            ActualWidth1 = 280,
                            Text = notifyMessage
                        };
            n.Notify();
            _notifyMessageObject = notifyMessageObject;
            n.Click += n_Click;
        }

        void n_Click(object sender, EventArgs e)
        {
            fwq.Invoke((EventHandler) delegate
                                          {
                                              fwq.Show();
                                              fwq.UpdateListView(_notifyMessageObject.FolderSidk__BackingField);
                                          });
        }

        public void UserLogOffMessage()
        {
            XtraMessageBox.Show(GlobalString.System_Exception_String.UserLogOffStr, GlobalString.System_Exception_String.MbxErrorStr, MessageBoxButtons.OK,
                                      MessageBoxIcon.Information);
            Environment.Exit(0);
        }


        public void UserRepeatLoginLogOff()
        {
            throw new NotImplementedException();
        }


        public void UpdateClientService(string version)
        {
            throw new NotImplementedException();
        }
    }
}
